<template>
  <div>
    <el-popover
      v-model="visible"
      placement="bottom"
      width="245"
      trigger="click"
      @show="handelShow">
      <div v-if="hasRouteAuth('chart:departEdit:color')" class="top-tool">
        <div v-if="isEditMode" class="button-text" @click="addColor()">添加渐变色</div>
        <div class="flex"/>
        <div class="button-text icon" @click="switchMode">{{ isEditMode?'取消管理':'管理' }} <img :src="ManageIcon" alt=""></div>
      </div>
      <div class="color-list scroll">
        <div v-for="(item, index) in bgColorList" :key="index" :class="{'active': item.id === selectBgId }" class="color-item">
          <div :style="getColor(item.colors)" class="color-item-inner" @click="handelClick(item)" />
          <img v-if="isEditMode && item.isCanEdit" :src="closeIcon" class="delete-icon" alt="" @click="deleteItem(item.id)">
        </div>
      </div>
      <div class="btn-list">
        <el-button type="primary" class="btn" size="small" @click="toSet()">确定</el-button>
        <el-button size="small" class="btn cancel-btn-style" @click="handleClose()">取消</el-button>
      </div>
      <div slot="reference" class="color-value"><div :style="getColor(selectColors)" class="inner-color" /></div>
    </el-popover>
    <SetColorDialog ref="colorDialogRef" :is-theme="false" @beforeAdd="open" @close="open" />
  </div>
</template>

<script>
/**
 * 渐变色获取
 */
import { mapState } from 'vuex';
import SetColorDialog from '@/components/ReportManage/setColorDialog';
import closeIcon from '@/assets/home/close.png';
import { deleteChartColor } from '@/api/reportManage';
import ElMessage, { showModal } from '@/utils/message';
import ManageIcon from '@/assets/chartsTheme/manage.png';
import { hasRouteAuth } from '@/utils/permission';

export default {
  components: { SetColorDialog },
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: {
      type: String | Number,
      default: ''
    }
  },
  data() {
    return {
      closeIcon, ManageIcon,
      visible: false,
      selectBgId: '',
      isEditMode: false
    };
  },
  computed: {
    ...mapState({
      'bgColorList': state => state.options.bgColorList
    }),
    selectColors() {
      const item = this.bgColorList.find(i => i.id === this.value);
      return item ? item.colors : this.bgColorList[0].colors;
    }
  },
  methods: {
    hasRouteAuth,
    getColor(colors) {
      if (!colors || colors.length !== 2) {
        return `background: #fff;`;
      }
      return `backgroundImage: linear-gradient(to top left, ${colors[0]}, ${colors[1]});`;
    },
    toSet() {
      this.$emit('change', this.selectBgId);
      this.visible = false;
    },
    open() {
      this.visible = true;
      this.$nextTick(() => {
        this.visible = true;
        this.isEditMode = true;
      });
    },
    handelClick(item) {
      if (this.isEditMode) {
        if (item.isCanEdit) this.addColor(item.id, item.colors);
        return;
      }
      this.selectBgId = item.id;
    },
    handelShow() {
      this.selectBgId = this.value;
    },
    handleClose() {
      this.visible = false;
      this.selectBgId = '';
      this.isEditMode = false;
    },
    switchMode() {
      this.isEditMode = !this.isEditMode;
    },
    addColor(id = null, colorsValue = []) {
      if (!id && this.bgColorList.length >= 28) {
        ElMessage.error('最多可新增20个渐变色');
        return;
      }
      this.$refs.colorDialogRef.oepnDialog(id, colorsValue);
    },
    updateColor() {
      this.$store.dispatch('GetColorList');
    },
    deleteItem(id) {
      showModal('是否确定删除', '', {
        iconClass: 'del-icon'
      }, true).then(() => {
        this.open();
        deleteChartColor({ id }).then(res => {
          ElMessage.success('删除成功');
          this.updateColor();
        });
      }).catch(() => {
        this.open();
        return false;
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.color-value {
  background-color: #fff;
  width: 64px;
  height: 32px;
  padding: 4px;
  border: 1px solid #e6e6e6;
  cursor: pointer;
  .inner-color {
    height: 100%;
    width: 100%;
    border: 1px solid #999;
  }
}
.top-tool {
  display: flex;
  padding-right: 16px;
  padding-left: 4px;
  line-height: 16px;
  margin-bottom: 3px;
  .flex {
    flex: 1;
  }
  .button-text {
    font-weight: 500;
  }
}
.color-list {
  display: flex;
  flex-wrap: wrap;
}
.color-item {
  margin-right: 6px;
  width: 46px;
  height: 46px;
  padding: 3px;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: 10px;
  position: relative;
  &:nth-child(4n) {
    margin-right: 0;
  }
  .delete-icon {
    width: 16px;
    height: 16px;
    position: absolute;
    top: -2px;
    right: -2px;
  }
  .color-item-inner {
    height: 100%;
    width: 100%;
    border-radius: 8px;
  }
  &.active {
    border-color: #406eff;
  }
}
.btn-list {
  margin-top: 10px;
  display: flex;
  justify-content: center;
}
.icon {
  display: flex;
  align-items: center;
  img {
    width: 16px;
    height: 16px;
    margin-left: 2px;
  }
}
.scroll {
  max-height: 300px;
  @include scrollBar($x: hidden, $y: auto);
}
</style>
